<?php
// +----------------------------------------------------------------------
// | flow-course / Controller.php    [ 2021/10/25 3:55 下午 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2021 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);


namespace app\api\admin;

use app\api\admin\v1\service\IndexService;
use app\library\BaseController;
use zhanshop\App;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Controller extends BaseController
{
    /**
     * finder
     * @param Request $request
     * @return mixed
     */
    protected function finder(Request $request, Response $response){
        $menuId = $request->getData('id');
        $service = App::make(IndexService::class);
        $action = $request->method();
        $service->permission((int)$request->getData('user.role_id'), (string)$menuId); // 验证菜单权限
        $result = $service->getFinder((string)$menuId)->$action($request, $response);
        return $result;
    }

    /**
     * 将结构简化
     * @param $uri
     * @param $data
     * @param int $fromFd
     * @return array
     */
    public function request($uri, $data){
        $request = [
            'uri' => $uri,
            'body' => $data
        ];
        return $request;
    }

    /**
     * 打包消息
     * @param mixed $body
     * @param string $uri
     * @param array $header
     * @return array
     */
    public function pack(mixed $data, string $uri = "result", array $header = [])
    {
        return json_encode([
            'uri' => $uri,
            'header' => $header,
            'body' => $data
        ], JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE);
    }
}